﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
#if UNITY_EDITOR
using UnityEditor;
#endif

public class BTNLoadTreePopWindow : EditorWindow
{

    #region Variable
    private string mSearchNode = "";
    static BTNLoadTreePopWindow curPopup;
    List<string> treesName;
    Rect toolbarRect = new Rect(0f, 0f, 0, 0);
    private readonly float mWidth = 400f;
    public Rect windowRect;
    private Vector2 mTreeScrollPos = Vector2.zero;
    //GUISkin viewSkin;
    #endregion

    public static void InitNodePopup()
    {
        if (BTreeEditorData.instance.btreesName == null)
        {
            EditorUtility.DisplayDialog("Note Message:", "There NO instance", "OK");
            return;
        }
        if (BTreeEditorData.instance.btreesName.Count == 0)
        {
            EditorUtility.DisplayDialog("Note Message:", "There NO Tree Data", "OK");
            return;
        }
        Rect windowRect = new Rect(400, 250, 400, 550); //子窗口的大小和位置
        curPopup = EditorWindow.GetWindowWithRect<BTNLoadTreePopWindow>(windowRect);

        curPopup.treesName = new List<string>();
        foreach(string key in BTreeEditorData.instance.btreesName)
        {
            curPopup.treesName.Add(key);
        }
        curPopup.treesName.Sort();
        curPopup.windowRect = windowRect;
        curPopup.titleContent = new GUIContent("Load Tree");
    }

    void OnGUI()
    {
        if (curPopup.treesName.Count == 0)
        {
            return;
        }
        List<string> nameList = Filter(curPopup.treesName, mSearchNode);

        GUILayout.BeginHorizontal();
        GUI.SetNextControlName("Search");
        this.mSearchNode = GUILayout.TextField(this.mSearchNode, GUI.skin.FindStyle("ToolbarSeachTextField"));
        GUI.FocusControl("Search");
        GUILayout.EndHorizontal();

       // toolbarRect = new Rect(0f, 15f + 20, mWidth, 25f);
       // Rect boxRect = new Rect(0f, toolbarRect.height, this.mWidth, (Screen.height - toolbarRect.height) - 21f + 10);
       // GUILayout.BeginArea(toolbarRect, EditorStyles.toolbar);
       //// GUILayout.BeginHorizontal();

       // GUILayout.Label("Filter");

       // if (GUILayout.Button("Clear"))
       // {
       // }
       // //GUILayout.EndHorizontal();
       // GUILayout.EndArea();

        GUILayout.BeginArea(new Rect(0, 0, windowRect.width, windowRect.height));
        float topSpace = 60;
        this.mTreeScrollPos = GUI.BeginScrollView(new Rect(0f, topSpace, windowRect.width, windowRect.height - topSpace), this.mTreeScrollPos,
                new Rect(0f, 0f, windowRect.width - 20f, nameList.Count * 19), false, true);
        foreach (var name in nameList)
        {
            Debug.Log(name);
            if (GUILayout.Button(name))
            {
                if(BTNodeUtils.LoadGraph(name))
                {
                    curPopup.Close();
                }
                
            }
        }

        GUI.EndScrollView();
        GUILayout.EndArea();

    }


    public List<string> Filter(List<string> list, string text)
    {
        if (string.IsNullOrEmpty(text))
        {
            return list;
        }

        List<string> result = new List<string>();
        foreach (var name in list)
        {
            if (name.ToUpper().Contains(text.ToUpper()))
            {
                result.Add(name);
            }
        }
        return result;
    }

    #region Utility Methods
    private readonly Color textColor = new Color(200f / 255f, 200f / 255f, 200f / 255f);
    private readonly Color textHighLightColor = new Color(200f / 255f, 200f / 255f, 0);

    public GUIStyle GetButtonStyle()
    {
        GUIStyle style = new GUIStyle();
        style.fontSize = 15;
        style.alignment = TextAnchor.MiddleLeft;
        GUIStyleState onHoverStyleState = new GUIStyleState();
        onHoverStyleState.textColor = textHighLightColor;
       // onHoverStyleState.background = BehaviorDesignerUtility.GetTexture("blue");
        style.hover = onHoverStyleState;

        GUIStyleState onNormalStyleState = new GUIStyleState();
        onNormalStyleState.textColor = textColor;
        style.normal = onNormalStyleState;
        return style;
    }
    #endregion
}
